const CALL_DELEGATE = function(...args) {
    this.call = this._createCall("sync");
	return this.call(...args);
};

class Hook {
    constructor(args = [], name = undefined) {
        this._args = args;
        this.name = name;
        this.taps = [];
        this._x = undefined;

        this.call = CALL_DELEGATE;
        this.compile = this.compile;
        this.tap = this.tap;
    }

    compile(options) {
        // 子类调用 compile，必须重写 compile 方法
        throw new Error("Abstract: should be overridden");
    }

    _createCall(type) {
        return this.compile({
            taps: this.taps,
            args: this._args,
            type: type
        });
    }

    tap(options, fn) {
        this._tap("sync", options, fn);
    }

    _tap(type, options, fn) {
        if (typeof options === "string") {
			options = {
				name: options.trim()
			};
		} else {
            // 省略
        }
        options = Object.assign({ type, fn }, options);    // {type: 'sync', fn: fn, name: 'sync'}
        this._insert(options);
    }

    _insert(item) {
        let i = this.taps.length;
        // 省略取 i 的过程
        this.taps[i] = item;
    }
}

module.exports = Hook